<template>
  <div class="login-container">
    <el-form class="login-form" autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left">
      <div class="title-container">
        <h3 class="title">管理员登录</h3>
      </div>
      <el-form-item prop="username">
        <span class="svg-container svg-container_login">
          <!-- <svg-icon icon-class="user" /> -->
          <i class="iconfont ali-username"></i>
        </span>
        <el-input name="username" type="text" v-model="loginForm.username" autoComplete="on" placeholder="手机号" />
      </el-form-item>
      <el-form-item prop="username">
        <span class="svg-container svg-container_login">
          <i class="iconfont ali-danwei"></i>
        </span>
        <el-select style="width:400px;" v-model="loginForm.company_id" placeholder="公司">
          <el-option v-for="item in companyList" :key="item.company_id" :label="item.company_name" :value="item.company_id"></el-option>
        </el-select>
      </el-form-item>
      <el-row type="flex" class="row-bg">
        <el-col :span="17">
          <div class="grid-content bg-purple">
            <el-form-item prop="code" style="width:68%">
              <span class="svg-container">
                <!-- <svg-icon icon-class="password" /> -->
                <i class="iconfont ali-ai-password"></i>
              </span>
              <el-input name="code" style="width:70%" :type="passwordType" @keyup.enter.native="handleLogin" v-model="loginForm.code" autoComplete="on" placeholder="验证码" />
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="grid-content bg-purple">
            <el-button type="primary" @click.prevent="getCode()" :disabled="isdis" style="height:40px; margin-top:5px;">{{inpval}}</el-button>
          </div>
        </el-col>
      </el-row>
      <el-button type="primary" style="width:100%;margin-bottom:30px; height:40px" :loading="loading" @click.native.prevent="handleLogin">登录</el-button>
    </el-form>

  </div>
</template>
<script>
import { sendCode, backstage_listCompany } from "@/api/login";
export default {
  name: 'login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (validateUsername == null) {
        callback(new Error('请输入正确的管理员用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('验证码长度应大于6'))
      } else {
        callback()
      }
    }
    return {
      companyList: [],
      inpval: '获取验证码',
      countdown: 30,
      isdis: false,
      loginForm: {
        username: '',
        code: '',
        company_id: 1
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        code: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'code',
      loading: false
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === '验证码') {
        this.passwordType = ''
      } else {
        this.passwordType = '验证码'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid && !this.loading) {
          this.loading = true
          this.$store.dispatch('LoginByUsername', this.loginForm).then(() => {
            this.loading = false
            this.$notify({
              title: '提示',
              message: '登陆成功',
              type: 'success'
            });
            this.$router.push({ path: '/' })
          }).catch(_ => {
            let err = _.data;
            this.loading = false
            if (err && err.status === 101) {
              this.$message.error(err.msg)
            }
          })
        } else {
          return false
        }
      })
    },
    async getCode() {
      if (this.loginForm.username == '') {
        this.$message.error('请填写手机号');
        return
      }
      if (this.loginForm.username.length < 11) {
        this.$message.error('请填写有效的手机号');
        return
      }
      this.Code();
      await sendCode({ phone: this.loginForm.username })
    },
    Code() {
      if (this.countdown == 0) {
        this.isdis = false;
        this.inpval = "获取验证码";
        this.countdown = 60;
        return;
      } else {
        this.isdis = true;
        this.inpval = "已发送(" + this.countdown + ")";
        this.countdown--;
      }
      setTimeout(() => {
        this.Code();
      }, 1000);
    },
    async initCompanyList(){
      let {data} = await backstage_listCompany();
      if(data.status === 100){
        this.companyList = data.data.rows.map(item => {
          return {company_id: item.id, ...item}
        });
        if(this.companyList.length > 0){
          this.loginForm.company_id = this.companyList[0].company_id;
        }
      }
    }
  },
  created(){
    this.initCompanyList();
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg:#2d3a4b;
$light_gray:#eee;


/* reset element-ui css */

.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #fff !important;
      }
    }
  }
  .el-button--medium {
    padding: 15px 20px;
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: $bg;
  .login-form {
    position: absolute;
    left: 0;
    right: 0;
    width: 520px;
    padding: 35px 35px 15px 35px;
    margin: 120px auto;
  }
  .tips {

    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 40px;
    display: inline-block;
    &_login {
      font-size: 20px;
    }
  }
  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      font-weight: 400;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
